package hw.pjbk;

import java.util.Vector;

import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.content.SharedPreferences;

public class MultiChoiceQuestion extends Activity implements OnClickListener {
	private TextView questionTextView;
	private Vector<MCQuestion> questions;
	private RadioGroup opts;
	private MCQuestion currentQuestion;
	private int noOfQuestions;

	private Quiz1Application application;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multi_choice_question);
		questionTextView = (TextView) findViewById(R.id.question);
		opts = (RadioGroup) findViewById(R.id.options);
		Log.i("MCQ", "MultiChoice starts");
		Button sub = (Button) findViewById(R.id.submit);
		sub.setOnClickListener(this);
		initialiseQuestions();
		currentQuestion = questions.get(0);

		application = (Quiz1Application) this.getApplication();

		// get the number of questions specified in the app preferences
		SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(getBaseContext());
		noOfQuestions = Integer.parseInt(pref.getString("noofquestions", "1"));

		// if the app preferences specify a larger number than the actual number
		// of available questions, limit the number to the max no. of questions.
		if (noOfQuestions > questions.size())
			noOfQuestions = questions.size();

		generateQuestion();
	}

	private void generateQuestion() {
		opts.removeAllViews();
		questionTextView.setText(currentQuestion.getQuestion());
		int i = 1;
		for (String ans : currentQuestion.getAnswers()) {
			RadioButton rb = new RadioButton(this);
			rb.setText(ans);
			rb.setId(i++);
			opts.addView(rb);
		}
		// clear check from previous answers
		opts.clearCheck();
	}

	public void onClick(View arg0) {
		int id;
		id = opts.getCheckedRadioButtonId();
		Log.i("HIST", "Button " + opts.getCheckedRadioButtonId() + " checked");
		if (id == -1) {
			Log.i("HIST", "No radio button checked");
			// if no selection remind user with some toast
			Toast toast = Toast.makeText(getApplicationContext(),
					"Please select an answer", Toast.LENGTH_SHORT);
			toast.show();

		} else {

			Log.i("Quiz", "Answer " + id + " submitted");
			application.incrementQuestionsAttempted();
			if (currentQuestion.isCorrect(id)) {
				Toast.makeText(this, "correct", Toast.LENGTH_SHORT).show();
				application.incrementQuestionsCorrect();
			} else {
				Toast.makeText(this, "wrong", Toast.LENGTH_SHORT).show();
			}
			generateQuestion();
			// move to the next question if the current question isn't the last
			int indexOfCurrentQuestion = questions.indexOf(currentQuestion);
			if ((indexOfCurrentQuestion < noOfQuestions - 1)) {
				currentQuestion = questions.get(indexOfCurrentQuestion + 1);
				generateQuestion();
			} else {
				finish();
			}
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		// record which radio button has been checked in the instance state
		outState.putInt("SelectedItemID", opts.getCheckedRadioButtonId());
		Log.i("HELLO", "Hello 1");
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// if the instance state has a previously recorded radio button
		// selection, reselect it
		super.onRestoreInstanceState(savedInstanceState);
		if (savedInstanceState != null) {
			if (savedInstanceState.getInt("SelectedItemID") > -1) {
				RadioButton rb = (RadioButton) opts
						.findViewById(savedInstanceState
								.getInt("SelectedItemID"));
				rb.setSelected(true);
				Log.i("HELLO", "Hello 2");
			}
		}
	}

	private void initialiseQuestions() {
		questions = new Vector<MCQuestion>();
		MCQuestion q1 = new MCQuestion("Who conquered England in 1066?");
		q1.addAnswer("Harold Godwin", false);
		q1.addAnswer("Edward the Confessor", false);
		q1.addAnswer("William the Conqueror", true);
		q1.addAnswer("Alfred the Great", false);
		questions.add(q1);

		MCQuestion q2 = new MCQuestion(
				"Which country gained independence from the British Empire in 1947?");
		q2.addAnswer("Cameroon", false);
		q2.addAnswer("India", true);
		q2.addAnswer("Rhodesia", false);
		q2.addAnswer("South Africa", false);
		questions.add(q2);

		MCQuestion q3 = new MCQuestion(
				"What year did the English Civil War end?");
		q3.addAnswer("1626", false);
		q3.addAnswer("1701", false);
		q3.addAnswer("1642", false);
		q3.addAnswer("1646", true);
		questions.add(q3);

		MCQuestion q4 = new MCQuestion(
				"Who was the first British prime minister after World War II?");
		q4.addAnswer("Neville Chamberlain", false);
		q4.addAnswer("Winston Churchill", false);
		q4.addAnswer("Clement Attlee", true);
		q4.addAnswer("Anthony Edent", false);
		questions.add(q4);

		MCQuestion q5 = new MCQuestion(
				"Who were the Celtic-speaking peoples living in France & Belgium during Roman Times?");
		q5.addAnswer("The Gauls", true);
		q5.addAnswer("The Visigoths", false);
		q5.addAnswer("The Celts", false);
		q5.addAnswer("The Saxons", false);
		questions.add(q5);

		MCQuestion q6 = new MCQuestion(
				"Which wife of Henry VIII had already married twice before she became queen, and married for a fourth time after Henry?s death?");
		q6.addAnswer("Anne of Cleves", false);
		q6.addAnswer("Katherine Parr", true);
		q6.addAnswer("Jane Seymor", false);
		q6.addAnswer("Katherine Howard", false);
		questions.add(q6);

	}

}
